﻿using Knight.Common.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Web;


namespace Knight.Web.Infrastructure
{
    public static class UrlHepler
    {

        #region Ip(获取Ip)
        /// <summary>
        /// 获取Ip
        /// </summary>
        public static string Ip
        {
            get
            {
                var result = string.Empty;
                if (HttpContext.Current != null)
                    result = GetWebClientIp();
                if (result.IsEmpty())
                    result = GetLanIp();
                return result;
            }
        }

        /// <summary>
        /// 获取Web客户端的Ip
        /// </summary>
        private static string GetWebClientIp()
        {
            var ip = GetWebRemoteIp();
            foreach (var hostAddress in Dns.GetHostAddresses(ip))
            {
                if (hostAddress.AddressFamily == AddressFamily.InterNetwork)
                    return hostAddress.ToString();
            }
            return string.Empty;
        }

        /// <summary>
        /// 获取Web远程Ip
        /// </summary>
        private static string GetWebRemoteIp()
        {
            return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        }

        /// <summary>
        /// 获取局域网IP
        /// </summary>
        private static string GetLanIp()
        {
            foreach (var hostAddress in Dns.GetHostAddresses(Dns.GetHostName()))
            {
                if (hostAddress.AddressFamily == AddressFamily.InterNetwork)
                    return hostAddress.ToString();
            }
            return string.Empty;
        }

        #endregion

        /// <summary>
        /// 添加URL参数
        /// </summary>
        public static string AddParam(string url, string paramName, string value)
        {
            int index = url.IndexOf("?");
            if (index == -1)
            {
                url = url + "?" + paramName + "=" + value;
            }
            else
            {
                url = url + "&" + paramName + "=" + value;
            }
            return url;
        }
        /// <summary>
        /// 删除指定参数
        /// </summary>
        public static string DeleteAddParam(string url, string paramName)
        {
            string url1 = url;
            if (url.IndexOf(paramName) > 0)
            {
                if (url.IndexOf("&", url.IndexOf(paramName) + paramName.Length) > 0)
                {
                    url1 = url.Substring(0, url.IndexOf(paramName) - 1) + url.Substring(url.IndexOf("&", url.IndexOf(paramName) + paramName.Length) + 1);
                }
                else
                {
                    url1 = url.Substring(0, url.IndexOf(paramName) - 1);
                }
                return url1;
            }
            else
            {
                return url1;
            }
        }
        /// <summary>
        /// 更新URL参数
        /// </summary>
        public static string UpdateParam(string url, string paramName, string value)
        {
            string keyWord = paramName + "=";
            int index = url.IndexOf(keyWord) + keyWord.Length;
            int index1 = url.IndexOf("&", index);
            if (index1 == -1)
            {
                url = url.Remove(index, url.Length - index);
                url = string.Concat(url, value);
                return url;
            }
            url = url.Remove(index, index1 - index);
            url = url.Insert(index, value);
            return url;
        }
        /// <summary>
        /// 更新或添加URL参数
        /// </summary>
        public static string UpdateOrAddParam(string url, string paramName, string value, bool isRemovePage = false)
        {
            //如果选择去掉页数
            if (isRemovePage)
            {
                int pageIndex = url.IndexOf("page=");
                if (pageIndex > -1)
                {
                    string pageUrl = url;
                    pageUrl = pageUrl.Substring(pageIndex);
                    if (pageUrl.IndexOf("&") > -1)
                    {
                        pageUrl = pageUrl.Substring(0, pageUrl.IndexOf("&") + 1);
                    }
                    url = url.Replace(pageUrl, "");
                    if (url.IndexOf("?") == url.Length - 1 || url.IndexOf("&") == url.Length - 1)
                    {
                        url = url.Substring(0, url.Length - 1);
                    }
                }
            }

            string keyWord = paramName + "=";
            int index = url.IndexOf(keyWord);
            if (index < 0)
            {
                if (url.IndexOf("?") > -1)
                {
                    url += "&" + keyWord;
                }
                else
                {
                    url += "?" + keyWord;
                }
                index = url.IndexOf(keyWord) + keyWord.Length;
            }
            else
            {
                index = index + keyWord.Length;

            }
            int index1 = url.IndexOf("&", index);
            if (index1 == -1)
            {
                url = url.Remove(index, url.Length - index);
                url = string.Concat(url, value);
                return url;
            }
            url = url.Remove(index, index1 - index);
            url = url.Insert(index, value);
            return url;

        }

    }
}
